package pl.edu.agh.jemo.gui;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * Window used to view single plot.
 * Displays image based on given path. May be created multiple times to allow viewing of more than one chart, and comparison of results.
 * 
 * @author Marcin Zbijowski
 *
 */
public class ImageViewer extends JFrame {

	/**
	 * As JFrame is implementation of Serializable interface, it requires this field to be set.
	 */
	private static final long serialVersionUID = 3538477632724396177L;

	/**
	 * Creates and shows window with picture displayed on it's center.
	 * 
	 * @param imgPath Path to file with population plot picture.
	 */
	public ImageViewer(String imgPath) {
		setTitle("Full size population");
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		getContentPane().setLayout(new BorderLayout(0, 0));
		
		JPanel imgPanel = new JPanel();
		ImageIcon img = new ImageIcon(imgPath);
		
		Image imgRef = img.getImage();
		BufferedImage buffImg = new BufferedImage(imgRef.getWidth(null), imgRef.getHeight(null), BufferedImage.TYPE_INT_ARGB);
		Graphics g = buffImg.createGraphics();
		g.drawImage(imgRef, 0, 0, imgRef.getWidth(null), imgRef.getHeight(null), null);
		img = new ImageIcon(buffImg);
		
		JLabel imgLabel = new JLabel();
		imgLabel.setIcon(img);
		imgPanel.add(imgLabel);

		getContentPane().add(imgPanel, BorderLayout.CENTER);
		setSize(imgRef.getWidth(null), imgRef.getHeight(null));
		setLocationRelativeTo(null);
		setVisible(true);
		imgPanel.revalidate();
		imgPanel.repaint();
	}
}
